package graphicinterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * this class is a box used for page selection. It has a JLabel on the center of it and
 * two "arrows" indicating that the user can change the current page by using the arrows
 * of the keyboard.
 * This class is used by "ShowJPanelsOnManyPages".
 * @author Mina
 *
 */
public class MenuForPageSelection extends JPanel implements KeyListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected JLabel labelCurrentPage;
	private String currentPage;
	protected int currentPositionInPossiblePages;
	protected LinkedList<String> possiblePages;
	protected JLabel labelArrowLeft;
	protected JLabel labelArrowRight;
	
	private int howManyItemsInOnePage; //how many items needs to be shown on one page
	private int howManyItemsOnAllPages; //how many items will be shown on all pages
	
	public MenuForPageSelection(int howManyItemsInOnePage, int howManyItemsOnAllPages)
	{
		this.howManyItemsInOnePage = howManyItemsInOnePage;
		this.howManyItemsOnAllPages = howManyItemsOnAllPages;
		
		LinkedList<String> pagesNames = new LinkedList<String>();
		int howManyPagesShouldBeCreated = this.howManyPagesShouldBeCreated();
		for(int i = 0; i < howManyPagesShouldBeCreated; i++)
		{
			String onePageName = "page " + (i + 1); //we won't start from page 0
			pagesNames.add(onePageName);
		}
		this.createPanelForPageSelection(pagesNames);
	}
	
	public int getCurrentPage()
	{
		String[] separatePageAndNumber = currentPage.split(" "); 
		//the name of a page is always: "page X" with X = number of page.
		
		String number = separatePageAndNumber[1];
		return Integer.valueOf(number);
	}
	
	private void createPanelForPageSelection(LinkedList<String> pagesNames)
	{
		/*creates panel that has a centered label with current page and two arrows for page
		 transition*/
		
		this.possiblePages = pagesNames;
		
		this.setFocusable(true);
		
		ImageIcon iconLeftArrow =
				CreatesImageIcon.createImageIcon("images/stoppedarrowleft.gif",
																		"stopped arrow left");
		this.labelArrowLeft = new JLabel(iconLeftArrow);
		
		this.createLabelCurrentPage();
		this.currentPage = this.possiblePages.get(0);

		ImageIcon iconRightArrow =
				CreatesImageIcon.createImageIcon("images/stoppedarrowright.gif",
																		"stopped arrow right");
		this.labelArrowRight = new JLabel(iconRightArrow);
		
		this.add(labelArrowLeft);
		this.add(labelCurrentPage);
		this.add(labelArrowRight);
		
		this.addKeyListener(this);
		
	}
	
	public void createLabelCurrentPage()
	{
		this.labelCurrentPage = new JLabel(this.possiblePages.get(0));
		this.currentPositionInPossiblePages = 0;
		this.labelCurrentPage.setFont(new Font("Serif", Font.BOLD, 24));
		this.labelCurrentPage.setForeground(new Color(255, 201, 14));
		this.labelCurrentPage.setPreferredSize(new Dimension(300,30));
		this.labelCurrentPage.setHorizontalAlignment(SwingConstants.CENTER);
	}
	
	private int howManyPagesShouldBeCreated()
	{	
		int howManyPagesShouldBeCreated = 0;
		int counterOfItems = 0;  /*this counter will go from 1 to value determined by
										"howManyItemsInOnePage" and by going to 
										"howManyItemsInOnePage", the "howManyPagesShouldBeCreated" 
										will change*/
		
		for(int i = 0; i < this.howManyItemsOnAllPages; i++)
		{
			counterOfItems = counterOfItems + 1;
			
			if(counterOfItems == this.howManyItemsInOnePage)
			{
				howManyPagesShouldBeCreated = howManyPagesShouldBeCreated + 1;
				counterOfItems = 0;
			}
		}
		
		if(counterOfItems > 0)
		{
			/*some items will be shown on the last page. There aren't X items on last page
			 * (X = "howManyItemsInOnePage") but at least there are less than X items*/
			howManyPagesShouldBeCreated = howManyPagesShouldBeCreated + 1;
		}
		return howManyPagesShouldBeCreated;
	}

	@Override
	public void keyPressed(KeyEvent e) 
	{
		int key = e.getKeyCode();
        if(key == 37)
        {
        	//user clicked "left button" on keyboard
        	this.changeArrowImage("arrowleft");
        	if(this.currentPositionInPossiblePages == 0)
        	{
        		this.currentPositionInPossiblePages = this.possiblePages.size() - 1;
        		
        	}
        	else
        	{
        		this.currentPositionInPossiblePages = currentPositionInPossiblePages - 1;
        	}
        	
        	String newCurrentPage = 
        			this.possiblePages.get(currentPositionInPossiblePages);
        	labelCurrentPage.setText(newCurrentPage);
        	this.currentPage = newCurrentPage;
        	
        }
        else if(key == 39)
        {
        	//user clicked "left button" on keyboard
        	
        	this.changeArrowImage("arrowright");
        	if(this.currentPositionInPossiblePages == (possiblePages.size() - 1))
        	{
        		this.currentPositionInPossiblePages = 0;
        		
        	}
        	else
        	{
        		this.currentPositionInPossiblePages = currentPositionInPossiblePages + 1;
        	}
        	
        	String newCurrentPage = 
        			this.possiblePages.get(currentPositionInPossiblePages);
        	labelCurrentPage.setText(newCurrentPage);
        	this.currentPage = newCurrentPage;
        }
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void changeArrowImage(String whichArrow)
	{
			ImageIcon newIconArrow =
					CreatesImageIcon.createImageIcon("images/" + whichArrow + ".gif",
																				"an arrow moves");
			newIconArrow.getImage().flush();
			if(whichArrow.compareTo("arrowleft") == 0)
			{
				this.labelArrowLeft.setIcon(newIconArrow);
			}
			else
			{
				this.labelArrowRight.setIcon(newIconArrow);
			}
	}

}
